package com.my.xl.salary.billdemo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.my.xl.salary.billdemo.pojo.ConfigData;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DingDingService {
    @Autowired
    private ConfigData t;

    //获取access_token
    public String getAccess_token(){
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setCorpid(t.getCorpId());
        request.setCorpsecret(t.getCorpSecret());
        request.setHttpMethod("GET");
        OapiGettokenResponse response=null;
        try {
            response = client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        if (response!=null){
            if (response.getErrcode()==0&&response.getErrmsg().equals("ok")){
                return response.getAccessToken();
            }
        }
        return null;
    }

    //免登码code获取用户信息
    public OapiUserGetuserinfoResponse login(String access_token,String code){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
        OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
        request.setCode(code);
        request.setHttpMethod("GET");
        OapiUserGetuserinfoResponse response = null;
        try {
            if (access_token!=null){
                response = client.execute(request, access_token);
            }
        } catch (ApiException e) {
            System.out.println(e.getMessage()+"<===免登码code获取用户信息报错");
            e.printStackTrace();
        }
        return response;
    }

    //userId获取用户身份信息
    public OapiUserGetResponse getEmpInfoByUserId(String userid,String access_token){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
        OapiUserGetRequest request = new OapiUserGetRequest();
        request.setUserid(userid);
        request.setHttpMethod("GET");
        OapiUserGetResponse res_=null;
        try {
            if (access_token!=null){
                res_ = client.execute(request, access_token);
            }
        } catch (ApiException e) {
            System.out.println(e.getMessage()+"<======userId获取用户信息报错");
            e.printStackTrace();
        }
        return res_;
    }

    //企业通讯录员工搜索
    public JSONObject searchEmp(String character_, String access_token){
        DingTalkClient client = new DefaultDingTalkClient("https://eco.taobao.com/router/rest");
        CorpEmpSearchRequest req = new CorpEmpSearchRequest();
        req.setKeyword(character_);
        req.setOffset(0L);
        req.setSize(10L);
        CorpEmpSearchResponse rsp = null;
        try {
            if (access_token!=null){
                rsp = client.execute(req, access_token);
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        JSONObject js = new JSONObject();
        if (rsp!=null&&(!rsp.getResult().equals("")||rsp.getResult()!=null)){
            JSONObject jsonObject = JSON.parseObject(rsp.getResult());
            JSONArray list = jsonObject.getJSONArray("list");
            if (list.size()>0){
                String  userId = JSON.parseObject(list.get(0).toString()).getString("staffId");
                String name=JSON.parseObject(list.get(0).toString()).getString("name");
                js.put("userid",userId);
                js.put("name",name);
            }
        }else{
            js.put("userid","");
            js.put("name","");
        }
        return js;
    }

    //发送钉钉消息------推送消息
    public  void sendMsg(String userid,String content,String access_token){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setUseridList(userid);
        request.setAgentId(Long.parseLong(t.getAgentId()));//在钉钉内以谁的名义发送消息给员工或者领导
        request.setToAllUser(false);
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent(content);
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response rsp=null;
        try {
            if (access_token!=null){
                rsp = client.execute(request,access_token);
            }else{
                System.out.println("access_token为空!!!!!!");
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        if (rsp!=null){
            if (rsp.isSuccess()){
                System.out.println("发送消息成功!!!!!!");
            }else{
                System.out.println("消息发送失败@!!!!1");
            }
        }
    }








}
